package com.example.fresh_market.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.fresh_market.entity.User;

public class UserMapper {

    private Context mContext;
    private DatabaseHelper dbHelper;

    public UserMapper(Context context) {
        mContext = context;
        dbHelper = new DatabaseHelper(mContext);
    }

    public boolean save(User user) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("username", user.getUsername());
        values.put("password", user.getPassword());
        long newRowId = db.insert(DatabaseHelper.USER_TABLE, null, values);
        return newRowId != -1;
    }

    public boolean exist(String username) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        try (Cursor cursor = db.query(DatabaseHelper.USER_TABLE, null, "username=?", new String[]{username},
                null, null, null)) {
            return cursor.getCount() != 0;
        }
    }

    public User getUser(String username) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        return null;
    }
}
